package com.uinfor.service.impl;

import com.uinfor.dao.IMoudleDao;
import com.uinfor.entity.Moudle;
import com.uinfor.service.IAccountService;
import com.uinfor.service.IMoudleService;
import com.uinfor.service.IStorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @descripttion:
 * @Author: WZH
 * @Date :2021/2/5  14:21
 **/
@Service
@Slf4j
public class MoudleImpl implements IMoudleService {
    @Resource
    private IMoudleDao iMoudleDao;
    @Resource
    private IAccountService iAccountService;
    @Resource
    private IStorageService iStorageService;
    @Override
    @GlobalTransactional(name = "seata-test",rollbackFor = Exception.class)
    public void create(Moudle moudle) {
        log.info("----->开始新建订单");
        log.info(moudle.toString());
        iMoudleDao.create(moudle);
        log.info("---------->开始订单微服务调用库存，扣减");
        iStorageService.decrease(moudle.getProductId(),moudle.getCount());
        log.info("------------------>扣钱微服务，进行账户扣钱");
        Double money = moudle.getMoney().doubleValue();
        System.out.println(money);
        iAccountService.decrease(moudle.getUserId(),money);
        log.info("------------>订单状态改变");
        iMoudleDao.update(moudle.getUserId(),0);
        log.info("----------->订单结束");
    }

    @Override
    public void update(Long id, int status) {
         iMoudleDao.update(id,status);
    }
}
